home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / file-tra / fsp-2.7 / fsp-2 / fsp / bsd_src / print.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-07  |  4.5 KB  |  178 lines

  1. /*
  2.  * Copyright (c) 1989 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Michael Fischbein.
  7.  *
  8.  * Redistribution and use in source and binary forms are permitted
  9.  * provided that: (1) source distributions retain this entire copyright
  10.  * notice and comment, and (2) distributions including binaries display
  11.  * the following acknowledgement:  ``This product includes software
  12.  * developed by the University of California, Berkeley and its contributors''
  13.  * in the documentation or other materials provided with the distribution
  14.  * and in all advertising materials mentioning features or use of this
  15.  * software. Neither the name of the University nor the names of its
  16.  * contributors may be used to endorse or promote products derived
  17.  * from this software without specific prior written permission.
  18.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  19.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  20.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  21.  */
  22.  
  23. #ifndef lint
  24. static char sccsid[] = "@(#)print.c    5.22 (Berkeley) 5/10/90";
  25. #endif /* not lint */
  26.      
  27. #include "tweak.h"
  28.      
  29. #include <stdio.h>
  30.      
  31. #ifndef VMS
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #endif
  35.      
  36. #ifdef VMS
  37. #include "types.h"
  38. #include "param.h"
  39. #include "grp.h"
  40. #include "pwd.h"
  41. #include "utmp.h"
  42. #include "stat.h"
  43. #else
  44. #include <sys/param.h>
  45. #include <grp.h>
  46. #include <pwd.h>
  47. #include <utmp.h>
  48. #endif
  49.      
  50. #include "ls.h"
  51.      
  52. #define BLK(A) (((A)+1023)/1024)
  53.      
  54. static int printtype PROTO1(mode_t, mode)
  55. {
  56.   switch(mode & S_IFMT) {
  57.     case S_IFDIR:
  58.       (void)putchar('/');
  59.       return(1);
  60.   }
  61.   return(0);
  62. }
  63.  
  64. /*
  65.  * print [inode] [size] name
  66.  * return # of characters printed, no trailing characters
  67.  */
  68. static int printaname PROTO1(LS *, lp)
  69. {
  70.   int chcnt;
  71.   
  72.   chcnt = 0;
  73.   
  74.   if (f_inode) {
  75.     printf("%5lu ", lp->lstat.st_ino);
  76.     chcnt += 6;
  77.   }
  78.   
  79.   if (f_size) {
  80.     printf("%4ld ", BLK(lp->lstat.st_size));
  81.     chcnt += 5;
  82.   }
  83.   
  84.   printf("%s", lp->name); chcnt += strlen(lp->name);
  85.   
  86.   if (f_type) chcnt += printtype(lp->lstat.st_mode);
  87.   
  88.   return(chcnt);
  89. }
  90.  
  91. void printscol PROTO2(LS *, stats, int, num)
  92. {
  93.   for (; num--; ++stats) {
  94.     printaname(stats);
  95.     putchar('\n');
  96.   }
  97. }
  98.  
  99. static int printtime PROTO1(time_t, ftime)
  100. {
  101.   int i;
  102.   char *longstring;
  103.   
  104.   longstring = (char *)ctime((long *)&ftime);
  105.   for (i = 4; i < 11; ++i) (void)putchar(longstring[i]);
  106.   
  107. #define    SIXMONTHS    ((365 / 2) * 24 * 60 * 60)
  108.  
  109.   if (ftime + SIXMONTHS > time((time_t *)NULL))
  110.     for (i = 11; i < 16; ++i) (void)putchar(longstring[i]);
  111.   else {
  112.     (void)putchar(' ');
  113.     for (i = 20; i < 24; ++i) (void)putchar(longstring[i]);
  114.   }
  115.   (void)putchar(' ');
  116. }
  117.  
  118. void printlong PROTO2(LS *, stats, int, num)
  119. {
  120.   char *modep;
  121.   
  122.   if (f_total) (void)printf("total %lu\n", stats[0].lstat.st_btotal);
  123.   for (; num--; ++stats) {
  124.     if (f_inode) printf("%6lu ", stats->lstat.st_ino);
  125.     if (f_size ) printf("%4ld ", BLK(stats->lstat.st_size));
  126.     modep = ((S_IFDIR & stats->lstat.st_mode)) ? "drwxrwxrwx" : "-rw-rw-rw-" ;
  127.     
  128.     (void)printf("%s %3u %-*s ", modep, stats->lstat.st_nlink, 8, "nobody");
  129.     if (f_group) printf("%-*s ", 8, "nobody");
  130.     else printf("%8ld ", stats->lstat.st_size);
  131.     if (f_accesstime) printtime(stats->lstat.st_atime);
  132.     else if (f_statustime) printtime(stats->lstat.st_ctime);
  133.     else printtime(stats->lstat.st_mtime);
  134.     printf("%s", stats->name);
  135.     if (f_type) printtype(stats->lstat.st_mode);
  136.     putchar('\n');
  137.   }
  138. }
  139.  
  140. #define    TAB    8
  141.  
  142. void printcol PROTO2(LS *, stats, int, num)
  143. {
  144.   extern int termwidth;
  145.   register int base, chcnt, cnt, col, colwidth;
  146.   int endcol, numcols, numrows, row;
  147.   
  148.   colwidth = stats[0].lstat.st_maxlen;
  149.   if (f_inode) colwidth += 6;
  150.   if (f_size) colwidth += 5;
  151.   if (f_type) colwidth += 1;
  152.   
  153.   colwidth = (colwidth + TAB) & ~(TAB - 1);
  154.   if (termwidth < 2 * colwidth) {
  155.     printscol(stats, num);
  156.     return;
  157.   }
  158.   
  159.   numcols = termwidth / colwidth;
  160.   numrows = num / numcols;
  161.   if (num % numcols) ++numrows;
  162.   
  163.   if (f_size && f_total) printf("total %lu\n", stats[0].lstat.st_btotal);
  164.   for (row = 0; row < numrows; ++row) {
  165.     endcol = colwidth;
  166.     for (base = row, chcnt = col = 0; col < numcols; ++col) {
  167.       chcnt += printaname(stats + base);
  168.       if ((base += numrows) >= num) break;
  169.       while ((cnt = (chcnt + TAB & ~(TAB - 1))) <= endcol) {
  170.     (void)putchar('\t');
  171.     chcnt = cnt;
  172.       }
  173.       endcol += colwidth;
  174.     }
  175.     putchar('\n');
  176.   }
  177. }
  178.